# Copyright (c) 2023 az13js
# 基金分类/classify-funds is licensed under Mulan PSL v2.
# You can use this software according to the terms and conditions of
# the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#          http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF
# ANY KIND,EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
# NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v2 for more details.

"""
从网站www.szse.cn下载Excel文件。先下载所有基金列表，再根据列表的基金代码去下载
具体每个基金的净值。
"""

import requests
import random
import os
import openpyxl

def download_excel_file(download_address, file_name):
    """
    请求download_address进行下载，保存为file_name。定制了header，不通用。
    参数：
        download_address 下载请求的地址
        file_name 保存的文件名
    返回值：
        无
    """
    headers = {
        'Accept': 'text/html,*/*',
        'Accept-Language': 'en',
        'Host': 'www.szse.cn',
        'Referer': 'http://www.szse.cn/market/product/list/all/index.html',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36 Edg/109.0.1518.78'
    }
    r = requests.get(download_address, headers=headers, stream=True)
    with open(file_name, 'wb') as fp:
        for chunk in r.iter_content(4096):
            fp.write(chunk)

def download_fund(fund_file_name):
    """
    从www.szse.cn下载所有基金的Excel。
    参数：
        fund_file_name 保存的文件名
    返回值：
        无
    """
    download_address = 'http://www.szse.cn/api/report/ShowReport?SHOWTYPE=xlsx&CATALOGID=1105&TABKEY=tab1&random={random_number}'.format(random_number=random.random())
    print('The file will be downloaded from the following address:')
    print(download_address)
    download_excel_file(download_address, fund_file_name)
    print('Already downloaded. Step 1 completed.')

def download_details(fund_file_name):
    """
    根据指定的所有基金的Excel，下载所有的净值，保存到data目录。data必须存在。
    参数：
        fund_file_name 所有基金列表Excel
    返回值：
        无
    """
    book = openpyxl.load_workbook(fund_file_name)
    sheet = book.active

    r = 2
    while True:
        cell = sheet.cell(row=r, column=1)
        if cell.value is None:
            break
        fund_code = cell.value.strip()
        if '' == fund_code:
            break
        print('Downloading file {fund_code}.xlsx'.format(fund_code=fund_code))
        download_excel_file('http://www.szse.cn/api/report/ShowReport?SHOWTYPE=xlsx&CATALOGID=1785_child&TABKEY=tab1&txtDm={fund_code}&random={random_number}'.format(fund_code=fund_code, random_number=random.random()), 'data{sep}{fund_code}.xlsx'.format(sep=os.sep, fund_code=fund_code))
        r = r + 1
    print('Already downloaded. Step 2 completed.')

if __name__ == '__main__':
    fund_file_name = 'data' + os.sep + 'fund.xlsx'
    download_fund(fund_file_name)
    download_details(fund_file_name)
